package com.hospital.hospital.controller;

import com.hospital.hospital.bean.Department;
import com.hospital.hospital.service.impl.DepartmentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class DepartmentController {

    @Autowired
    private DepartmentServiceImpl departmentService;

    @RequestMapping("/managerUi")
    public String managerUi(){
        return "manager";
    }

    @RequestMapping("/depUi")
    public String depUi(){
        return "department";
    }

    @RequestMapping("/second")
    public String second(Integer page,Model model){
        System.out.println(page);
        List<Department> departments = departmentService.queryByPage(page);
        model.addAttribute("departments",departments);
        return "mainDep";
    }

    @RequestMapping("/docUi")
    public String docUi(){
        return "hhhhh";
    }


    //     <a href="/addDep">科室添加</a>
    @RequestMapping("/addDep")
    public String addDep(Department department){
        try {
            departmentService.addDep(department);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "department";
    }
    //     <a href="/deleteDep">科室删除</a>
    @RequestMapping("/deleteDep")
    public String deleteDep(Integer[] departmentIds,Model model) {
        try {
            for (Integer departmentId : departmentIds) {
                System.out.println(departmentId);
                departmentService.deleteDep(departmentId);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        List<Department> departments = null;
        try {
            departments = departmentService.queryAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("departments",departments);
        return "mainDep";
    }
    //     <a href="/setDep">科室修改</a>
    @RequestMapping("/setDep")
    public String setDep(Integer departmentIds,Model model){
        System.out.println(departmentIds);
        Integer departmentId = departmentIds;
        try {
            Department department = departmentService.queryById(departmentId);
            System.out.println(department);
            model.addAttribute("department",department);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "setDep";
    }
    //   切换状态
    @RequestMapping("/qh")
    public String qh(Integer[] departmentIds,Model model){
        try {
            for (Integer departmentId : departmentIds) {
                departmentService.qh(departmentId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<Department> departments = null;
        try {
            departments = departmentService.queryAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("departments",departments);
        return "mainDep";
    }
    //     <a href="/queryDep">科室查询</a>
    @RequestMapping("/queryDep")
    public String queryDep(String likestr,Model model) throws Exception {
        System.out.println(likestr);
        List<Department> departments = departmentService.queryLike(likestr);
        model.addAttribute("departments",departments);
        return "queryDep";
    }
    //     <a href="/mainDep">所有科室</a>
    @RequestMapping("/mainDep")
    public String mainDep(Model model) throws Exception {
        List<Department> departments = departmentService.queryAll();
        model.addAttribute("departments",departments);
        return "mainDep";
    }

    @RequestMapping("/add")
    public String add(Department department) throws Exception {
        departmentService.addDep(department);
        return "department";
    }

    @RequestMapping("/set")
    public String set(Department department,Model model) throws Exception {
        System.out.println(department);
        departmentService.set(department);
        List<Department> departments = null;
        try {
            departments = departmentService.queryAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("departments",departments);
        return "mainDep";
    }
}
